package controllers

import (
	"encoding/json"
	"gitee.com/lzqysoft/ims/models"
	"github.com/astaxie/beego"
)

type MajorController struct {
	CommonController
}

// URLMapping ...
func (c *MajorController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Post ...
// @Title Post
// @Description create SysMajor
// @Param	body		body 	models.SysMajor	true		"body for SysMajor content"
// @Success 201 {int} models.SysMajor
// @Failure 403 body is empty
// @router / [post]
func (c *MajorController) Post() {
	user := c.User
	tdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrToken)
	}
	m := models.NewSysMajor()
	m.Creater, m.Updater = user.Abbr, user.Abbr
	if err = json.Unmarshal(tdata, m); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	if _, err = models.AddSysMajor(m); err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	c.RetSuccess("")
}

// GetOne ...
// @Title Get One
// @Description get SysMajor by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.SysMajor
// @Failure 403 :id is empty
// @router /:id [get]
func (c *MajorController) GetOne() {
	idStr := c.Ctx.Input.Param(":id")
	v, err := models.GetSysMajorById(idStr)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	c.RetSuccess(v)
}

// GetAll ...
// @Title Get All
// @Description get SysMajor
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.SysMajor
// @Failure 403
// @router /GetAll [post]
func (c *MajorController) GetAll() {

	var params QueryParams

	//PageSize  int64             `json:"pagesize"`
	//Page      int64             `json:"page"`
	//SearchKey string            `json:"searchKey"`  // 暂不使用此字段
	//SortBy    []string          `json:"sortby"`
	//Order     []string          `json:"order"`
	//Fields    []string          `json:"fields"`
	//Query     map[string]string `json:"query"`
	// fields: col1,col2,entity.col3
	tdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrToken)
	}
	if err = json.Unmarshal(tdata, &params); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}

	l, count, err := models.GetAllSysMajor(params.Query, params.Fields, params.SortBy, params.Order, (params.Page-1)*params.PageSize, params.PageSize)
	if err != nil {
		c.Data["json"] = err.Error()
		beego.Debug(err)
		c.RetError(ErrInputData)
	}

	c.RetSuccessListAndCount(l, count)
}

// Put ...
// @Title Put
// @Description update the SysMajor
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.SysMajor	true		"body for SysMajor content"
// @Success 200 {object} models.SysMajor
// @Failure 403 :id is not int
// @router /:id [put]
func (c *MajorController) Put() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	v := models.SysMajor{Id: idStr}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if err := models.UpdateSysMajorById(&v); err == nil {
			c.Data["json"] = "OK"
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the SysMajor
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *MajorController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	if err := models.DeleteSysMajor(idStr); err == nil {
		c.Data["json"] = "OK"
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}
